/**
* @Title: insuredBlacklist_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @author EX-XIEYUWEN001
* @date 2014年10月29日 下午14:46:06
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('InsuredBlackListQueryCtrl', ['$scope', '$http','$stateParams','BaseURL', 'Modal','$modal','IntegrityMedicalService','CommunityService', 
	                                             function($scope, $http,$stateParams, baseUrl, Modal,$modal,IntegrityMedicalService,CommunityService){
		$scope.form={};
		$scope.auditOpinionList=[{code:'01',label:'待审核'},{code:'02',label:'已审核'},{code:'03',label:'已撤回'},{code:'04',label:'已退回'}];
		
		 var cellTemplate = '<a ng-show="data.processState ==\'03\'" ng-click="operation(data);">修改</a>' +
         '<a ng-show="data.processState ==\'01\'" ng-click="operation(data);">撤回</a>'+
		 '<a ng-show="data.processState ==\'04\'" ng-click="operation(data);">修改</a>'+
		 '<a ng-show="data.processState ==\'02\'" ng-click="operation(data);">查看</a>';
		 
		 var nameCellTemplate='<a ng-show="data.processState ==\'03\'" ng-click="editorBlackPersonModal(data);" ng-bind="data.name"></a>' +
         '<a ng-show="data.processState ==\'01\'" ng-click="lookInfo(data);" ng-bind="data.name"></a>'+
		 '<span ng-show="data.processState ==\'04\'" ng-bind="data.name"></span>'+
		 '<span ng-show="data.processState ==\'02\'" ng-bind="data.name"></span>';
		 
		//表头
        $scope.columns = [
							{label:'姓名',map:'name',width:'10%',cellTemplate:nameCellTemplate},
							{label:'性别',map:'gender',width:'5%'},
							{label:'保险号',map:'personCard',width:'7%'},
							{label:'案发时间',map:'strHappenDate',width:'8%'},
							{label:'有效期',map:'expirtDate',width:'15%'},
							
							{label:'电话号码',map:'phoneNumber',width:'7%'},
							{label:'手机号码',map:'mobileNumber',width:'7%'},
							{label:'关注级别',map:'attentionLevel',width:'7%'},
							{label:'关注时限',map:'attentionTimeName',width:'7%'},
							{label:'违规情节',map:'illegal',width:'17%'},
							{label:'状态',map:'processStateName',width:'5%'},
							{label:'操作',map:'opertion',width:'5%',cellTemplate:cellTemplate}
						];
        $scope.agreementListParam = {
                page:"1",
                pageSize:"20"
            };
        //重置函数
        $scope.onReset=function(){
        	$scope.form={};
        	$scope.form.happenDate={
        			start:IntegrityMedicalService.getStartDate(),
					end:IntegrityMedicalService.getEndDate()
        	};
        }
        
        $scope.lookInfo = function(data){

            	$scope.editor=false;
            	$scope.blackId=data.id;
            	auditPopDialogFun();
            	
          
        }
        
        
        $scope.operation = function(data){

            if(data.processState=="03") //已经撤销
            {  
            	$scope.editorBlackPersonModal(data);
            }else if(data.processState=="01"){//撤销
            	cancelBuinsessProcess(data);
            }else if(data.processState=="04"){ //已经退回
            	getRebackReason(data);
            }else if(data.processState=="02"){ //已经审核
            	
            	$scope.editor=false;
            	$scope.blackId=data.id;
            	auditPopDialogFun();
            	
            }

        }
        
        
        /**
         * 查看弹出框
         */
        function auditPopDialogFun(){
       	 var scope = $scope.$new();
    		var modalInstance = $modal.open({
    		      templateUrl: baseUrl+'views/hcpms/achievements/integrity/insuredBlackCheckDialog_view.html',
    		      backdrop:'static',
    			  size:'sm',
    			  scope:scope,
    			  controller:'InsuredBlackCheckProcessCtrl'
    		      });
    			  
    		}
        
        
        /**
         * 撤销操作
         */
        function cancelBuinsessProcess(data){
        	var id=data.id;
            $http({
				method : 'post',
				cache : false,
				url : baseUrl + 'integrityBlack/cancelBuinsessProcess.do',
				params :{
					id:id
				}
			}).success(function(response, status, headers, config) {
				if(response.flag){
					query();
					Modal.alert(response.msg);
				}else{
					Modal.alert(response.msg);
				}
			});
        	
        	
        }
        
        //查询按钮事件
		$scope.onSubmit = function(){
			$scope.currentPage = 1;
			$scope.agreementListParam.page = 1;
			$scope.agreementListParam.name=$scope.form.name;
			$scope.agreementListParam.personCard=$scope.form.personCard;
			$scope.agreementListParam.processState=$scope.form.processState;
			if($scope.form.happenDate){
				$scope.agreementListParam.startDate=$scope.form.happenDate.start;
				$scope.agreementListParam.endDate=$scope.form.happenDate.end;
			}else{
				$scope.agreementListParam.startDate='';
				$scope.agreementListParam.endDate='';
			}
			query();
		}
		//弹出查询框
		$scope.queryUserOnClick=function(){
			var scope = $scope.$new();
			var modalInstance = $modal.open({
			      templateUrl: baseUrl+'views/hcpms/achievements/integrity/selectUserInfoDialog_view.html',
			      backdrop:'static',
				  size:'sm',
				  scope:scope,
				  controller: 'selectUserModalCtrl'
			    });
		}
		//上传文件
		$scope.uploadFileOnClick=function(){
			var scope = $scope.$new();
			var modalInstance = $modal.open({
			      templateUrl: baseUrl+'views/hcpms/achievements/integrity/UploadUserInfoDialog_view.html',
			      backdrop:'static',
				  size:'sm',
				  scope:scope,
				  controller: 'uploadUserModalCtrl'
			    });
		}
		/**
		 * 下载导入模板
		 */
		$scope.downLoadOnClick=function(){
			   var path="";
               window.location = baseUrl + 'views/hcpms/file/blackTemplate.xls';
		}
		
		/**
		 * 增加参保人信息
		 */
		$scope.addUserOnClick=function(){
			$scope.opertionType='Add';
			var scope = $scope.$new();
			var modalInstance = $modal.open({
			      templateUrl: baseUrl+'views/hcpms/achievements/integrity/addUserInfoDialog_view.html',
			      backdrop:'static',
				  size:'sm',
				  scope:scope,
				  controller: 'InsuredBlackListAddCtrl'
			    });
		}
		
		/**
		 * 映射性别
		 */
		function isGender(gender){
			if(gender=='男'){
				return "M";
			}else{
				return "F";
			}
		}
		
		/**
		 * 解析有效日期
		 */
		function isExpirtDate(expirtDate){
			var obj={};
			var str=expirtDate.split("至");
			obj.start=str[0];
			obj.end=str[1];
			return obj;
			
		}
		
		/**
		 * 获取退回的原因
		 */
		function getRebackReason(data){
			 $http({
	 				method : 'post',
	 				cache : false,
	 				url : baseUrl + 'integrityBlackAudit/loadRebackReason.do',
	 				params :{
	 					id:data.id
	 				}
	 			}).success(function(response, status, headers, config) {
	 				  if(response){
	 					$scope.backReason=response.opinionDesc;
	 	            	$scope.auditBackProcessModal(data);
	 				  }
	 			});
		}
		
		
		/**
		 * 审核岗位退回的编辑参保人信息
		 */
		$scope.auditBackProcessModal=function(data){
			$scope.opertionType='Editor';
			var dateStr = data.realExpirtDate;
            var dateStart = data.realExpirtDate.split('至')[0];
            var dateEnd = data.realExpirtDate.split('至')[1];
			$scope.person={
					name:data.name,
					gender:isGender(data.gender),
					happenDate:data.strHappenDate,
					personCard:data.personCard,
					cardType:data.cardType,
					cardNo:data.cardNo,
					inBlackReason:data.inBlackReason,
					dateStart:dateStart,
                    dateEnd:dateEnd,
					pingy:data.alPhabetic,
					phoneNumber:data.phoneNumber,
					mobileNumber:data.mobileNumber,
					attentionMidId:data.attentionMidId,
					illegal:data.attentionLevelId,
					attentionLevel:data.attentionMidId,
					attentionTime:data.attentionTime
			};
			$scope.id=data.id;
			var scope = $scope.$new();
			var modalInstance = $modal.open({
			      templateUrl: baseUrl+'views/hcpms/achievements/integrity/auditBackUserInfoDialog_view.html',
			      backdrop:'static',
				  size:'sm',
				  scope:scope,
				  controller: 'InsuredBlackListAuditBackCtrl'
			    });
		}
		
		
		/**
		 * 编辑参保人信息
		 */
		$scope.editorBlackPersonModal=function(data){
			$scope.opertionType='Editor';
			var dateStr = data.realExpirtDate;
            var dateStart = data.realExpirtDate.split('至')[0];
            var dateEnd = data.realExpirtDate.split('至')[1];
			$scope.person={
					name:data.name,
					gender:isGender(data.gender),
					happenDate:data.strHappenDate,
					personCard:data.personCard,
					cardType:data.cardType,
					cardNo:data.cardNo,
					inBlackReason:data.inBlackReason,
					dateStart:dateStart,
                    dateEnd:dateEnd,
					pingy:data.alPhabetic,
					phoneNumber:data.phoneNumber,
					mobileNumber:data.mobileNumber,
					attentionMidId:data.attentionMidId,
					illegal:data.attentionLevelId,
					attentionLevel:data.attentionMidId,
					attentionTime:data.attentionTime
			};
			$scope.id=data.id;
			var scope = $scope.$new();
			var modalInstance = $modal.open({
			      templateUrl: baseUrl+'views/hcpms/achievements/integrity/addUserInfoDialog_view.html',
			      backdrop:'static',
				  size:'sm',
				  scope:scope,
				  controller: 'InsuredBlackListAddCtrl'
			    });
		}
		$scope.commonQuery=function(){
			 query();
		}
		//提交函数
		function query(){
		$scope.loading = true;
          var params = $scope.agreementListParam;
          $http({
				method : 'post',
				cache : false,
				url : baseUrl + 'integrityBlack/queryIntegrityBlackData.do',
				params :params
			}).success(function(response, status, headers, config) {
				 $scope.loading = false;
				if(response){
					$.each(response.result,function(index,item){
						var processState=item.processState;
						var attentionTime=item.attentionTime;
						if(processState=='01'){
							item.processStateName='待审核';
						}else if(processState=='02'){
							item.processStateName='已审核';
						}else if(processState=='03'){
							item.processStateName='已撤回';
						}else if(processState=='04'){
							item.processStateName='已退回';
						}
						if(attentionTime=='0'){
							item.attentionTimeName='终身';	
						}else if(attentionTime){
							item.attentionTimeName=attentionTime+"年";	
						}
						
						
					});
					$scope.tableData = response.result;
	                $scope.totalCount = response.total;
				}
				
				
			});
		}
		
		var startDate = $stateParams.startDate;
		var endDate = $stateParams.endDate;
		
		if (!startDate 
				&& !endDate) {
				//默认日期 当前日期的前一个月
				CommunityService.initDate("yyyy-MM-dd").success(function(res) {
					endDate = res.formatDate;
					startDate = CommunityService.lastYear(endDate);
					$scope.form.happenDate = {
							start: startDate,
							end: endDate
					};
					query();
				});
			}
			else {
				if (startDate 
					&& !endDate) {
					endDate = CommunityService.backwardYear(startDate);
				} 
				if (!startDate 
					&& endDate) {
					startDate = CommunityService.lastYear(endDate);
				}
				$scope.form.happenDate = {
						start: startDate,
						end: endDate
				};
				query();
			}
		//分页监听
		$scope.$on('pageTool.skipPage',function(e,v){
			 var id=e.targetScope.id;
			 
		     if(id=='parentPageTool'){
		    	 
		    	    $scope.currentPage = v;
					$scope.agreementListParam.page = v;
					query();
		    	 
		     }
			
		});
		
			
	}]);
})();